/**
 * Created by asus on 2017/9/24.
 */
public class Sorting {

    public static void selectionSort(Comparable[] data){
        int min;
        for (int index=0;index<data.length-1;index++){
            min = index;
            for (int scan=index+1;scan <data.length;scan++)
                if (data[scan].compareTo(data[min])<0)
                    min = scan;

            swap(data,min,index);
        }
    }
    private static void swap(Comparable[] data,int index1,int index2){

        Comparable temp = data[index1];
        data[index1] = data[index2];
        data[index2] = temp;
    }
    public static void quickSort(Comparable[] data,int min ,int max){
        int pivot;
        if (min <max){
            pivot = partition(data,min,max);
            quickSort(data,min,pivot-1);
            quickSort(data,pivot+1,max);
        }
    }
    private static int partition(Comparable[] data,int min,int max){
        Comparable partitionValue = data[min];
        int left = min;
        int right = max;
        while (left<right){
            while (data[left].compareTo(partitionValue)<=0&&left<right)
                left++;
            while (data[right].compareTo(partitionValue)>0)
                right--;
            if (left<right)
                swap(data,left,right);
        }
        swap(data,min,right);
        return right;
    }
}
